package com.example.demo.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.handler.annotation.MessageMapping;
import org.springframework.messaging.handler.annotation.Payload;
import org.springframework.messaging.handler.annotation.SendTo;
import org.springframework.messaging.simp.SimpMessagingTemplate;
import org.springframework.messaging.simp.annotation.SendToUser;
import org.springframework.messaging.simp.annotation.SubscribeMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class StompController {
    @Autowired
    private SimpMessagingTemplate template;

//    @MessageMapping("/greeting")
//    public void greetings(@Payload String data) {
//        String msg = "return data:" + data;
//        template.convertAndSend("/queue/subscribe", msg);
//    }

    @SubscribeMapping ("/subscribe")
    public String subscribe() {
        return "subscribe success";
    }
    @MessageMapping("/greeting")
    @SendTo("/queue/greetings")
    public void greetings(@Payload String data) {
        String msg = "return data:" + data;
        System.out.println(msg);
        template.convertAndSend("/queue/subscribe", msg);
    }
    @MessageMapping("/message")
    @SendToUser("/topic/subscribe")
    public String sendToUser(String message) {
        return message +" from /users/topic/subscribe";
    }

}